package org.originit.factory.support;

import org.originit.exception.BeansException;
import org.originit.factory.BeanFactory;
import org.originit.factory.config.BeanDefinition;
import org.originit.factory.config.DefaultSingletonBeanRegistry;


public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String name) throws BeansException {
        return getBean(name,new Object[]{});
    }

    @Override
    public Object getBean(String name,Object... args) throws BeansException {
        Object singleton = getSingleton(name);
        if (singleton != null) {
            return singleton;
        }
        BeanDefinition beanDefinition = getBeanDefinition(name);
        return createBean(name,beanDefinition,args);
    }

    protected abstract Object createBean(String name, BeanDefinition beanDefinition,Object[] args) throws BeansException;

    protected abstract BeanDefinition getBeanDefinition(String name) throws BeansException;
}
